package GUI;
import CTRLayer.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Order extends JFrame {

	private Controller ctr;
	private NewOrder no;
	private JPanel contentPane;
	private JTextField txtCustomer;
	private JTextField txtDate;
	private JTable table;

	/**
	 * Create the frame.
	 */
	public Order() {
		setResizable(false);
		setTitle("Order");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 330);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCustomer = new JLabel("Customer ID:");
		lblCustomer.setBounds(10, 11, 70, 14);
		contentPane.add(lblCustomer);
		
		txtCustomer = new JTextField();
		txtCustomer.setBounds(90, 8, 86, 20);
		contentPane.add(txtCustomer);
		txtCustomer.setColumns(10);
		
		JLabel lblDate = new JLabel("Date:");
		lblDate.setBounds(10, 36, 70, 14);
		contentPane.add(lblDate);
		
		txtDate = new JTextField();
		txtDate.setBounds(90, 33, 86, 20);
		contentPane.add(txtDate);
		txtDate.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnSearch.setBounds(216, 32, 89, 23);
		contentPane.add(btnSearch);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 61, 424, 158);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "Customer ID", "Total"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, String.class, Double.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
			boolean[] columnEditables = new boolean[] {
				false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(2).setResizable(false);
		scrollPane.setViewportView(table);
		
		JButton button = new JButton("Add new");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				no = new NewOrder();
				no.setVisible(true);
			}
		});
		button.setBounds(10, 233, 115, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("Show information");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = table.getSelectedRow();
				if(SelRowIndex==-1)
				{
					JOptionPane.showMessageDialog(null,
						    "You have to chose one from the list",
						    "Warning",
						    JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String idSelected = (String) table.getValueAt(SelRowIndex, 0);
					JOptionPane.showMessageDialog(null,
					    ctr.orderInfo(idSelected));
				}
			}
		});
		button_1.setBounds(158, 233, 115, 23);
		contentPane.add(button_1);
		
		JButton button_2 = new JButton("Back to Main Menu");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainMenu.getInstance().setVisible(true);
				dispose();
			}
		});
		button_2.setBounds(145, 267, 144, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("Delete");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = table.getSelectedRow();
				if(SelRowIndex==-1)
				{
					JOptionPane.showMessageDialog(null,
						    "You have to chose one from the list",
						    "Warning",
						    JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String idSelected = (String) table.getValueAt(SelRowIndex, 0);
					ctr.deleteOrder(idSelected);
					JOptionPane.showMessageDialog(null,
					    "Deleted!"); 
				}
			}
		});
		button_3.setBounds(319, 233, 115, 23);
		contentPane.add(button_3);
	}
}
